package com.app.hmst.main;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.WindowManager;
import android.widget.Toast;

public class Utils {
	private boolean doubleBackToExitPressedOnce= true;
	private Context _context;	
	// constructor
	public Utils(Context context) {
		this._context = context;
	}

	/*
	 * getting screen width
	 */
	@SuppressWarnings("deprecation")
	@SuppressLint("NewApi")
	public int getScreenWidth() {
		int columnWidth;
		WindowManager wm = (WindowManager) _context
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();

		final Point point = new Point();
		try {
			display.getSize(point);
		} catch (java.lang.NoSuchMethodError ignore) { // Older device
			point.x = display.getWidth();
			point.y = display.getHeight();
		}
		columnWidth = point.x;
		return columnWidth;
	}
	public void onBackPressed(View hostView,final Fragment childFragment,final Activity act){
		hostView.setFocusableInTouchMode(true);
		hostView.requestFocus();

		hostView.setOnKeyListener(new OnKeyListener() {
		        @Override
		        public boolean onKey(View v, final int keyCode, KeyEvent event) {
		                if (event.getAction() == KeyEvent.ACTION_DOWN) {
		                    if (keyCode == KeyEvent.KEYCODE_BACK) {
		                    	//If childFragment is null exit program
		                    	if(childFragment==null&&act!=null){
		                    		AlertDialog.Builder builder1 = new AlertDialog.Builder(_context);
		                            builder1.setMessage(_context.getString(R.string.quitprogram_message));
		                            builder1.setCancelable(true);
		                            builder1.setPositiveButton(_context.getString(R.string.homestay_sync_ok),new DialogInterface.OnClickListener() {
		                                public void onClick(DialogInterface dialog, int id) {
		                                    dialog.cancel();
		                                    act.finish();		                        			
		                                }
		                            });
		                            builder1.setNegativeButton(_context.getString(R.string.cancel_message),new DialogInterface.OnClickListener() {
		                                public void onClick(DialogInterface dialog, int id) {
		                                    dialog.cancel();
		                                }
		                            });
		                            AlertDialog alert11 = builder1.create();
		                            alert11.show();	      
		                    	}else{
		                    		switchFragment(childFragment);		                    		
		                    	}
		                        
		                    return true;
		                    }
		                }
		                return false;
		            }
		        });
	}
	private void switchFragment(Fragment fragment) {
		if (_context == null)
			return;

		if (_context instanceof HomeStayMainActivity) {
			HomeStayMainActivity fca = (HomeStayMainActivity) _context;
			fca.switchContent(fragment);
		}
	}
	@SuppressLint("NewApi")
	public static boolean isNotEmpty(String str) {
	    return (str != null && !str.isEmpty());
	}
}
